package com.kust.smarhome.background.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.smarhome.background.mapper.CommodityMapper;
import com.kust.smarhome.background.mapper.ShopMapper;
import com.kust.smarhome.background.model.Commodity;
import com.kust.smarhome.background.model.Shop;
import com.kust.smarhome.background.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-31
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public List<Map<String, String>> getCollectData() {

        List<Commodity> commodities = commodityMapper.selectList(null);
        List<Map<String, String>> collectData = new ArrayList<>();
        for (Commodity commodity : commodities) {
            List<Shop> shops = shopMapper.selectList(new QueryWrapper<Shop>()
                    .eq("commodity_id", commodity.getId()));
            if (shops == null)
                continue;

            Integer shopNum = 0;
            for (Shop shop : shops) {
                shopNum += shop.getCommodityNumber();
            }
            Map<String,String> map = new HashMap<>();
            map.put("name", commodity.getName());
            map.put("value", shopNum.toString());

            collectData.add(map);
        }

        return collectData;
    }
}
